﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public static class UIUtility
{
    public static void RefreshElements<T>(T elementPrefab, int elementCount, Transform elementGroup, Action<T, int> refreshElement) where T: Component
    {
        elementPrefab.gameObject.SetActive(false);
        Transform parent = elementPrefab.transform.parent;
        if ((elementPrefab.transform.parent == elementGroup) && elementPrefab.gameObject.scene.IsValid())
        {
            elementPrefab.transform.SetParent(null);
        }
        for (int i = 0; i < elementCount; i++)
        {
            Transform transform = (i >= elementGroup.childCount) ? null : elementGroup.GetChild(i);
            if (transform == null)
            {
                transform = UnityEngine.Object.Instantiate<T>(elementPrefab).transform;
                transform.transform.SetParent(elementGroup);
            }
            transform.gameObject.SetActive(true);
            if (refreshElement != null)
            {
                refreshElement(transform.GetComponent<T>(), i);
            }
        }
        for (int j = elementCount; j < elementGroup.childCount; j++)
        {
            elementGroup.GetChild(j).gameObject.SetActive(false);
        }
        if (elementPrefab.transform.parent != parent)
        {
            elementPrefab.transform.SetParent(parent);
        }
    }

    public static void RefreshElements<T>(List<T> caches, int count, T prefab, Transform groupTsm, Action<T, int> refreshElement) where T: Component
    {
        prefab.gameObject.SetActive(false);
        while (caches.Count < count)
        {
            caches.Add(null);
        }
        for (int i = 0; i < count; i++)
        {
            if (caches[i] == null)
            {
                caches[i] = UnityEngine.Object.Instantiate<T>(prefab);
            }
            T local = caches[i];
            Transform transform = local.transform;
            transform.SetParent(groupTsm);
            transform.localPosition = transform.localPosition.SetZ(0f);
            transform.localScale = Vector3.one;
        }
        for (int j = 0; j < caches.Count; j++)
        {
            if (caches[j] != null)
            {
                T local2 = caches[j];
                local2.gameObject.SetActive(j < count);
            }
        }
        for (int k = 0; k < count; k++)
        {
            if ((caches[k] != null) && (refreshElement != null))
            {
                refreshElement(caches[k], k);
            }
        }
    }

    public static void SetChildrenTextFont(Component parent, Font font)
    {
        foreach (Text text in parent.GetComponentsInChildren<Text>())
        {
            text.font = font;
        }
    }

    public static void SetChildrenTextFontSize(Component parent, int fontSize)
    {
        foreach (Text text in parent.GetComponentsInChildren<Text>())
        {
            text.fontSize = fontSize;
        }
    }
}

